//
//  CPMainTabbarViewController.swift
//  weibo
//
//  Created by zcp on 16/5/10.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

class CPMainTabbarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //创建自定义tabbar
        let mainTarBar = CPMainTabBar()
       //传递闭包
        //使用kvc赋值,把自定义TarBar赋值给当前控制器的TarBar
        setValue(mainTarBar, forKey: "tabBar")
        
        //定义闭包
        let callback = {()->() in
        
            print("111")
        }
        //传递闭包
        mainTarBar.callback = callback
        //添加四个控制器,并包装一层navigation控制器
      
        
        let homeVC = CPHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        let discoveryVc = CPDiscoveryViewController()
        addChildViewController(discoveryVc, title: "发现", imageName: "tabbar_discover")
        
        let messageVC = CPMessageViewController()
        addChildViewController(messageVC, title: "信息", imageName: "tabbar_message_center")
        
        let profileVC = CPProfileTableViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
    }

    
    /**
    添加四个控制器,并包装一层navigation控制器

    */
    func addChildViewController(Controller: UIViewController,title: String,imageName: String) {
        
        let nav = UINavigationController(rootViewController: Controller)
        Controller.title = title
        Controller.tabBarItem.image = UIImage(named: imageName)
        self.addChildViewController(nav)
        Controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        Controller.tabBarItem.selectedImage = UIImage(named: imageName+"_selected")
    }
   

}
